package com.bdqn.t382.controller;

import com.bdqn.t382.entity.Brand;
import com.bdqn.t382.entity.VehicleComplaint;
import com.bdqn.t382.entity.vehiclecomplaintsAndBrand;
import com.bdqn.t382.service.CarService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class VehicleComplaintsController {
    @Resource
    private CarService carService;

    // 处理获取所有品牌请求
    @RequestMapping("/getBrands")
    public String getBrands(Model model) {
        List<Brand> brands = carService.getAllBrands();
        model.addAttribute("brands", brands);
        return "complaints";
    }

    // 处理根据品牌获取车辆投诉请求
    @RequestMapping("/selectAllByName")
    public String selectAllByName(@RequestParam(required = false) String brandName, Model model) {
        List<vehiclecomplaintsAndBrand> complaints = carService.getComplaintsByBrandName(brandName);
        model.addAttribute("complaints", complaints);

        List<Brand> brands = carService.getAllBrands();
        model.addAttribute("brands", brands);
        return "complaints";
    }

    // 处理添加投诉请求
    @RequestMapping("/addComplaint")
    public String addComplaint(@ModelAttribute vehiclecomplaintsAndBrand complaint, Model model) {
        List<Brand> brands = carService.getAllBrands();
        model.addAttribute("brands", brands);
        try {
            carService.addComplaint(complaint);
            model.addAttribute("message", "添加成功");
        }catch (Exception e) {
            model.addAttribute("message", "添加失败");
        }
        return "addComplaint";
    }

    //跳转至添加投诉页面
    @RequestMapping("/addTable")
    public String addTable() {
        return "addComplaint";
    }

//    // 处理添加投诉请求
//    @RequestMapping("/complaints")
//    public String addComplaint(@ModelAttribute VehicleComplaint complaint, Model model) {
//        try {
//            carService.addComplaint(complaint);
//            model.addAttribute("message", "添加成功");
//        } catch (Exception e) {
//            model.addAttribute("message", "添加失败");
//        }
//        return "result"; // 返回 Thymeleaf 模板名称，用于显示结果
//    }

//    // 处理修改投诉状态请求
//    @RequestMapping("/status")
//    public String updateComplaintStatus(@ModelAttribute VehicleComplaint complaint, Model model) {
//        try {
//            carService.updateComplaintStatus(complaint);
//            model.addAttribute("message", "修改成功");
//        } catch (Exception e) {
//            model.addAttribute("message", "修改失败");
//        }
//        return "result"; // 返回 Thymeleaf 模板名称，用于显示结果
//    }
}
